package strategy

import (
	"gvb_server/global"
	"gvb_server/plugins/qiniu"
	"io"
	"mime/multipart"
	"path"
)

type QiNiuUploadStrategy struct {
}

func (QiNiuUploadStrategy) Upload(file *multipart.FileHeader) (filePath string, err error) {
	openFile, err := file.Open()
	if err != nil {
		global.Log.Errorf("open file error: %s", err)
		return "", err
	}
	byteData, err := io.ReadAll(openFile)
	qiuNiuPath := path.Join(global.Config.QiNiu.Path, file.Filename)
	filePath, err = qiniu.UploadImage(byteData, qiuNiuPath)
	if err != nil {
		global.Log.Errorf("七牛云保存文件失败: %s", file.Filename)
		return "", err
	}
	return filePath, err
}
